const Express = require("express");
const UserServer = require("../../servers/UserInfo");
const ResponseHelper = require("../ResponseHelper");
const router = Express.Router();
const auth = require("../../tools/auth"); //鉴权
const LogServer = require("../../servers/LogInfo");

/**
 * 获取所有用户
 */
router.get("/listAll", async (req, res) => {
  try {
    const result = await UserServer.getAllAdmin();
    ResponseHelper.sendData(result, res);
  } catch (err) {
    ResponseHelper.sendErr(err, res);
  }
});

/**
 * 分页模糊 查询用户
 */
router.get("/getUserList", async (req, res) => {
  try {
    const { page, limit, name, level, searchLevel } = req.query;
    const result = await UserServer.find(
      page,
      limit,
      name ? name : null,
      level ? level : null,
      searchLevel
    );
    ResponseHelper.sendData(result, res);
  } catch (err) {
    console.log("err", err);
    ResponseHelper.sendErr(err, res);
  }
});

/**
 * 添加用户
 */
router.post("/addUser", async (req, res) => {
  try {
    const result = await UserServer.addAdmin(req.body);
    ResponseHelper.sendData(result, res);
  } catch (err) {
    console.log("err", err);
    ResponseHelper.sendErr(err, res);
  }
});

/**
 * 更新用户
 */
router.post("/updateUser/:id", async (req, res) => {
  const userId = req.params.id;
  const userObj = req.body;
  try {
    const result = await UserServer.updateAdmin(userId, userObj);
    ResponseHelper.sendData(result, res);
  } catch (err) {
    ResponseHelper.sendErr(err, res);
  }
});

/**
 * 根据id获取用户
 */
router.get("/getUser/:id", async (req, res) => {
  try {
    const userId = req.params.id;
    const result = await UserServer.getAdminById(userId);
    ResponseHelper.sendData(result, res);
  } catch (err) {
    ResponseHelper.sendErr(err, res);
  }
});
/**
 * 根据user获取用户
 */
router.get("/getUserByAcc/:user", async (req, res) => {
  try {
    const user = req.params.user;
    const result = await UserServer.getAdminByUser(user);
    ResponseHelper.sendData(result, res);
  } catch (err) {
    ResponseHelper.sendErr(err, res);
  }
});
/**
 * 根据level查询user
 */
router.get("/getAdminByLevel/:level", async (req, res) => {
  try {
    const level = req.params.level;
    const result = await UserServer.getAdminByLevel(level);
    ResponseHelper.sendData(result, res);
  } catch (err) {
    ResponseHelper.sendErr(err, res);
  }
});
/**
 * 根据id删除用户
 */
router.get("/delUser/:id", async (req, res) => {
  try {
    const userId = req.params.id;
    const result = await UserServer.deleteAdmin(userId);
    ResponseHelper.sendData(result, res);
  } catch (err) {
    ResponseHelper.sendErr(err, res);
  }
});
/**
 * 批量刪除用户
 */
router.post("/bulkDelUser", async (req, res) => {
  try {
    const result = await UserServer.bulkDelete(req.body);
    ResponseHelper.sendData(result, res);
  } catch (err) {
    ResponseHelper.sendErr(err, res);
  }
});

/**
 *登录
 */
router.post("/checkUserPassword", async (req, res) => {
  try {
    const { user, passwd } = req.body;
    const result = await UserServer.login(user, passwd);
    LogServer.add({ name: result.name, type: "登录", event: "登录系统" });
    ResponseHelper.sendData(result, res);
  } catch (err) {
    console.log("err", err);
    ResponseHelper.sendErr(err, res);
  }
});

/**
 *登出
 */
router.get("/loginOut/:name", async (req, res) => {
  try {
    const name = req.params.name;
    LogServer.add({ name: name, type: "登出", event: "登出系统" });
    ResponseHelper.sendData("登出", res);
  } catch (err) {
    ResponseHelper.sendErr(err, res);
  }
});

module.exports = router;
